package ACWing.Greed;
//104. 货仓选址

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author :chenjie
 * @date :Created 2023/1/15 21:56
 */
public class WarehouseLocation {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[]arr=new int[n];
        for (int i = 0; i < n; i++) {
            arr[i]=sc.nextInt();
        }
        Arrays.sort(arr);
        int x=arr[n/2];
        long sum=0;
        for (int i = 0; i < n; i++) {
            if(arr[i]>=x){
                sum+=(arr[i]-x);
            }else {
                sum+=(x-arr[i]);
            }
        }
        System.out.println(sum);
    }
}
